package com.proyecto.track.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.proyecto.track.bo.Coment;
import com.proyecto.track.bo.Task;
import com.proyecto.track.bo.UserPerson;
import com.proyecto.track.service.TaskService;
import com.proyecto.track.service.UserPersonService;

public class CommentAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private Task task;
	private List<Coment> comments = new ArrayList<Coment>();
	private Long idTask;
	private Coment comment;
	@Autowired
	private TaskService taskService;
	@Autowired
	private UserPersonService userPersonService;
	
	public String createComment(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Long accountId = (Long) session.get("accountId");
		UserPerson usuario = userPersonService.searchUserByAccount(accountId);
		this.task = taskService.searchTask(task.getId());
		comment.setTask(task);
		comment.setUserPerson(usuario);
		task.getComments().add(comment);
		taskService.modifyTask(task);
		usuario.getComments().add(comment);
		userPersonService.modifyUserPerson(usuario);
		this.comments = task.getComments();
		return SUCCESS;
	}
	
	public String loadComment(){
		this.task = taskService.searchTask(idTask);
		this.comments = task.getComments();
		return SUCCESS;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Coment getComment() {
		return comment;
	}

	public void setComment(Coment comment) {
		this.comment = comment;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public UserPersonService getUserPersonService() {
		return userPersonService;
	}

	public void setUserPersonService(UserPersonService userPersonService) {
		this.userPersonService = userPersonService;
	}

	public List<Coment> getComments() {
		return comments;
	}

	public void setComments(List<Coment> comments) {
		this.comments = comments;
	}

	public Long getIdTask() {
		return idTask;
	}

	public void setIdTask(Long idTask) {
		this.idTask = idTask;
	}
	
}
